﻿using System;
using System.Data;
using System.Data.Entity.Infrastructure;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using PiiSWebAPI.Models;

namespace PiiSWebAPI.Controllers
{
    public class EvaluationController : ApiController
    {
        private readonly PiiSDataModelContainer _db = new PiiSDataModelContainer();

        //// GET api/Evaluation
        //public IEnumerable<Evaluation> GetEvaluations()
        //{
        //    var evaluationset = _db.EvaluationSet.Include(e => e.Movie).Include(e => e.Person);
        //    return evaluationset.AsEnumerable();
        //}

        //// GET api/Evaluation/5
        //public Evaluation GetEvaluation(int id)
        //{
        //    Evaluation evaluation = _db.EvaluationSet.Find(id);
        //    if (evaluation == null)
        //    {
        //        throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.NotFound));
        //    }

        //    return evaluation;
        //}

        // PUT api/Evaluation/5
        public HttpResponseMessage PutEvaluation(int id, Evaluation evaluation)
        {
            if (!ModelState.IsValid)
            {
                return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState);
            }

            if (id != evaluation.Id)
            {
                return Request.CreateResponse(HttpStatusCode.BadRequest);
            }

            _db.Entry(evaluation).State = EntityState.Modified;

            try
            {
                _db.SaveChanges();
            }
            catch (DbUpdateConcurrencyException ex)
            {
                return Request.CreateErrorResponse(HttpStatusCode.NotFound, ex);
            }

            return Request.CreateResponse(HttpStatusCode.OK);
        }

        // POST api/Evaluation
        public HttpResponseMessage PostEvaluation(Evaluation evaluation)
        {
            if (ModelState.IsValid)
            {
                var mark =
                    _db.EvaluationSet.SingleOrDefault(
                        q => q.MovieId == evaluation.MovieId && q.PersonId == evaluation.PersonId);

                Evaluation returnVal;
                if(mark != null)
                {
                    mark.EvaluationValue = evaluation.EvaluationValue;
                    returnVal = mark;
                }
                else
                {
                    _db.EvaluationSet.Add(evaluation);
                    returnVal = evaluation;
                }
                _db.SaveChanges();

                returnVal.EvaluationValue = returnVal.EvaluationValue == null ? null : returnVal.EvaluationValue + 1;



                HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.Created,
                                                                      new EvaluationModel
                                                                          {EvaluationValue = returnVal.EvaluationValue});
                response.Headers.Location = new Uri(Url.Link("DefaultApi", new { id = evaluation.Id }));
                return response;
            }
            return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState);
        }

        //// DELETE api/Evaluation/5
        //public HttpResponseMessage DeleteEvaluation(int id)
        //{
        //    Evaluation evaluation = _db.EvaluationSet.Find(id);
        //    if (evaluation == null)
        //    {
        //        return Request.CreateResponse(HttpStatusCode.NotFound);
        //    }

        //    _db.EvaluationSet.Remove(evaluation);

        //    try
        //    {
        //        _db.SaveChanges();
        //    }
        //    catch (DbUpdateConcurrencyException ex)
        //    {
        //        return Request.CreateErrorResponse(HttpStatusCode.NotFound, ex);
        //    }

        //    return Request.CreateResponse(HttpStatusCode.OK, evaluation);
        //}

        protected override void Dispose(bool disposing)
        {
            _db.Dispose();
            base.Dispose(disposing);
        }
    }
}